<?php
class Login extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
	}
	public function index()
	{

		$this->form_validation->set_rules('username', 'username',  'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
		if ($this->form_validation->run()){
				
			redirect('transaction');
				
		}else{
			$data['title']='Log in';
			$this->load->view('templates/header',$data);
			$this->load->view('index',$data);
			$this->load->view('templates/footer',$data);
		}
	}

	public function check_database(){
		// Load the model
		$this->load->model('user_model');
		// Validate the user can login
		$result = $this->user_model->login();
		// Now we verify the result
		if($result){
			return true;
		}else{
			$this->form_validation->set_message('check_database', '*invalid user name or password');
			return false;
		}
	}

	private function check_isvalidated(){
		if($this->session->userdata('validated')){
			redirect('transaction');
		}
	}

}